
var app = new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue!'
  },
  methods: {
    printMessage: function (val) {
      this.message = val
    },
    reverseMessage: function () {
      this.message = this.message.split('').reverse().join('')
    }
  }
})

var wsUri = "ws://localhost:5555/";
var websocket;

function testWebSocket() {
  websocket = new WebSocket(wsUri);
  websocket.onopen = function (evt) {
    onOpen(evt)
  };
  websocket.onclose = function (evt) {
    onClose(evt)
  };
  websocket.onmessage = function (evt) {
    onMessage(evt)
  };
  websocket.onerror = function (evt) {
    onError(evt)
  };
}

function onOpen(evt) {
  writeToScreen("CONNECTED");
  doSend("WebSocket rocks");
}

function onClose(evt) {
  //writeToScreen("DISCONNECTED");
}

function onMessage(evt) {
  writeToScreen('RESPONSE: ' + evt.data);
  websocket.close();
}

function onError(evt) {
  writeToScreen('ERROR: ' + evt.data);
}

function doSend(message) {
  writeToScreen("SENT: " + message);
  websocket.send(message);
}

function writeToScreen(message) {
  app.printMessage(message)
}

testWebSocket();